﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       PansonicSerial
- 机器名称：       YTE
- 命名空间：       LD.Common.PLC
- 文 件 名：       PansonicSerial
- 创建时间：       2021/12/16 12:03:23
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using LD.Common;
using LdCommunication;
using LdCommunication.Profinet.Panasonic;
using System;
using System.IO.Ports;
using System.Threading.Tasks;
namespace LD.PLC
{
    /// <summary>
    /// 西门子串口
    /// </summary>
    public class PansonicSerial : PLCAbstract
    {
        private PanasonicMewtocol _plc;
        private Task _task;
        private bool _start;

        public override event Action<ConnStatus> ConnStatusEvent;
        private ConnStatus connStatus = ConnStatus.断线;
        public override ConnStatus ConnStatus
        {
            get { return connStatus; }
            set
            {
                connStatus = value;
                ConnStatusEvent?.Invoke(value);
            }
        }

        public PansonicSerial()
        {
            _plc = new PanasonicMewtocol(1);
        }
        private void Connection()
        {
            //LogHelper.Log(LogType.PC_PLC, "请求连接");

            _task = Task.Run(new Action(() =>
             {
                 _start = true;
                 while (_start)
                 {
                     if (ConnStatus == ConnStatus.断线)
                     {
                         OperateResult operate = _plc.Open();
                         if (operate.IsSuccess)
                             ConnStatus = ConnStatus.连接;
                     }
                     Task.Delay(3000).Wait();
                 }
                 _plc?.Close();
             }));
        }
        public override void Start(dynamic param)
        {
            HDParamModel hDParam = param as HDParamModel;
            //if (!hDParam.Validity)
            //    return;
            Parity parity = Parity.Even;
            if (hDParam.Parity.Trim().Length > 0)
                parity = (Parity)Enum.Parse(typeof(Parity), hDParam.Parity.Trim());
            _plc.SerialPortInni(hDParam.Address, hDParam.Port, 8, StopBits.One, parity);
            _plc.ReceiveTimeout = hDParam.TimeOut;
            Connection();
        }
        public override void Stop()
        {
            _start = false;
            _task?.Wait(200);
        }

        public override OperateResult<byte[]> Read(string address, ushort val)
        {
            return _plc.Read(address, val);
        }

        public override OperateResult<bool[]> ReadBool(string address, ushort val)
        {
            return _plc.ReadBool(address, val);
        }
        public override OperateResult<float> ReadFloat(string address)
        {
            return _plc.ReadFloat(address);
        }

        public override OperateResult<bool> ReadBool(string address)
        {
            return _plc.ReadBool(address);
        }

        public override OperateResult<string> ReadString(string address, ushort val)
        {
            return _plc.ReadString(address, val);
        }

        public override OperateResult<int> ReadInt(string address)
        {
            return _plc.ReadInt32(address);
        }

        public override OperateResult<int[]> ReadInt(string address, ushort val)
        {
            return _plc.ReadInt32(address, val);
        }

        public override OperateResult<short[]> ReadInt16(string address, ushort val)
        {
            return _plc.ReadInt16(address, val);
        }

        public override OperateResult<short> ReadInt16(string address)
        {
            return _plc.ReadInt16(address);
        }

        public override OperateResult Write(string address, ushort val)
        {
            return _plc.Write(address, val);
        }

        public override OperateResult Write(string address, int val)
        {
            return _plc.Write(address, val);
        }

        public override OperateResult Write(string address, bool val)
        {
            return _plc.Write(address, val);
        }

        public override OperateResult Write(string address, string val)
        {
            return _plc.Write(address, val);
        }

        public override OperateResult Write(string address, byte[] val)
        {
            return _plc.Write(address, val);
        }

        public override OperateResult<float[]> ReadFloat(string address, ushort val)
        {
            return _plc.ReadFloat(address, val);
        }
    }
}
